/*
Copyright (c) 2010,2011 k.schulz, cmueller

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

The Software shall be used for Good, not Evil.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

package de.altitudecustomcommands;

import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * This class is a representation of a virtual server. It includes attributs
 * like servername and listening port. It provides method to read those server attributs.
 *
 * @author k..schulz
 * @author c.mueller
 */
public class virtualServer {
    private int port = 0;
    private List<String> admins;
    private String map = "";
    private String mode = "";
    private int leftTeam;
    private int rightTeam;

    /**
     * This method will return the listening port for this virtual server.
     *
     * @return Returns the port of this virtual server
     */

    public int getPort() {
        return this.port;
    }


    private String name = null;

    /**
     * This method will return the name of this virtual server.
     *
     * @return Returns the name of this virtual server
     */

    public String getName() {
        return this.name;
    }


    private int slots = 0;

    /**
     * This method will return the amount of empty slots for this server. It will not
     * tell you how many players are on this server.
     *
     * @return Returns the maximum amount of players for this server.
     */

    public int getSlots() {
        return this.slots;
    }


    private int maxPing = 0;

    /**
     * This method will return the maximum ping before a player gets kicked from
     * your server.
     *
     * @return Returns the maximum ping for this server.
     */

    public int getMaxPing() {
        return this.maxPing;
    }


    private boolean hardWall = true;

    /**
     * This method let you check if this server got hardwalls.
     *
     * @return Returns true, if this server has hardwalls.
     */

    public boolean isHardWall() {
        return this.hardWall;
    }


    private String passwort = null;

    /**
     * This method returns the passwort for this virtual server.
     *
     * @return Returns the passwort for this server.
     */

    public String getPasswort() {
        return this.passwort;
    }

    /**
     * This method let you check if this virtual server is passwort protected.
     *
     * @return Returns true, if this server is passwort protected.
     */

    public boolean isPasswortProtected() {
        return (this.passwort == null || this.passwort.equals(""));
    }


    private boolean lanserver = false;

    /**
     * This method let you check if this virtual server is a lanserver.
     *
     * @return Returns true, if this server is a lan only server.
     */

    public boolean isLanserver() {
        return this.lanserver;
    }

    public String getMap() {
        return map;
    }

    public String getMode() {
        return mode;
    }

    public int getLeftTeam() {
        return leftTeam;
    }

    public int getRightTeam() {
        return rightTeam;
    }

    /**
     * Standard constructror. It constructs an empty server. Just needed to created a server array.
     */

    public virtualServer() {
    }

    /**
     * Constructor which constructs a new virtual server object, with all neccessary data, given as a <a href="https://jaxp-sources.dev.java.net/nonav/docs/api/index.html?org/w3c/dom/Node.html" target="_blank">Node object</a>.
     *
     * @param attributs A <a href="https://jaxp-sources.dev.java.net/nonav/docs/api/index.html?org/w3c/dom/Node.html" target="_blank">Node object</a> which got the attributs of your virtual server.
     */

    public virtualServer(Node attributs) {
        this.port = Integer.parseInt(attributs.getAttributes().getNamedItem("port").getNodeValue());
        this.lanserver = "true".equals(attributs.getAttributes().getNamedItem("lanServer").getNodeValue());
        this.hardWall = "true".equals(attributs.getAttributes().getNamedItem("hardcore").getNodeValue());
        this.maxPing = Integer.parseInt(attributs.getAttributes().getNamedItem("maxPing").getNodeValue());
        this.name = attributs.getAttributes().getNamedItem("serverName").getNodeValue();
        this.slots = Integer.parseInt(attributs.getAttributes().getNamedItem("maxPlayerCount").getNodeValue());
        this.passwort = attributs.getAttributes().getNamedItem("secretCode").getNodeValue();
        this.admins = new ArrayList<String>();
        NodeList nodes = attributs.getChildNodes();
        for (int i = 0; i < nodes.getLength(); i++) {
            if ("adminsByVaporID".equals(nodes.item(i).getNodeName())) {
                NodeList adminsVapors = nodes.item(i).getChildNodes();
                for (int j = 0; j < adminsVapors.getLength(); j++) {
                    Node item = adminsVapors.item(j);
                    if ("UUID".equals(item.getNodeName())) {
                        admins.add(adminsVapors.item(j).getAttributes().getNamedItem("UUID").getNodeValue());
                    }
                }
                break;
            }
        }
    }


    public boolean isAdminByVapor(String vaporId) {
        return (admins.contains(vaporId));
    }

    public void changeMap(String mode, String map, int leftTeam, int rightTeam) {
        this.map = map;
        this.mode = mode;
        this.leftTeam = leftTeam;
        this.rightTeam = rightTeam;
    }

    @Override
    public String toString() {
        return "virtualServer{" +
                "port=" + port +
                ", admins=" + admins +
                ", name='" + name + '\'' +
                ", slots=" + slots +
                ", maxPing=" + maxPing +
                ", hardWall=" + hardWall +
                ", passwort='" + passwort + '\'' +
                ", lanserver=" + lanserver +
                '}';
    }
}
